package com.huqing.icu.service.app.impl;

import com.huqing.icu.base.redis.Redis;
import com.huqing.icu.base.redis.annotation.RedisLock;
import com.huqing.icu.constanst.PostRedisConstant;
import com.huqing.icu.constants.CacheLockConstants;
import com.huqing.icu.enums.community.UserPostBehaviorEnum;
import com.huqing.icu.mapper.UserPostBehaviorMapper;
import com.huqing.icu.model.UserPostBehavior;
import com.huqing.icu.service.app.IUserPostPanelService;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description 用户帖子行为实现类
 * @Author huqing
 * @Date 2025/5/18 13:56
 **/
@Service
public class UserPostPanelServiceImpl implements IUserPostPanelService {

    private static final Logger logger = LoggerFactory.getLogger(IUserPostPanelService.class);

    @Resource
    private UserPostBehaviorMapper userPostBehaviorMapper;
    @Autowired
    private Redis redis;

    /**
     * 点赞数 +1 或 -1
     *
     * @param postId
     * @param incr
     * @return
     */
    @Override
    public Integer incrOrDecrLikeNum(Long postId, Boolean incr) {
        String key = PostRedisConstant.POST_LIKE + postId;
        if (incr) {
            redis.incr(key);
        } else {
            redis.decr(key);
        }
        return 1;
    }

    /**
     * 收藏数 +1 或 -1
     *
     * @param postId
     * @param incr
     * @return
     */
    @Override
    public Integer incrOrDecrFavoriteNum(Long postId, Boolean incr) {
        String key = PostRedisConstant.POST_FAVORITE + postId;
        if (incr) {
            redis.incr(key);
        } else {
            redis.decr(key);
        }
        return 1;
    }

    /**
     * 浏览人数 +1，浏览人数只能加，不能减
     *
     * @param userId
     * @param postId
     * @return
     */
    @RedisLock(key = CacheLockConstants.CommunityPostPrefix.USER_VIEW_POST, waitTime = 3000)
    @Override
    public Integer incrViewPersonNum(Long userId, Long postId) {

        try {
            //查询这个帖子是否浏览过
            UserPostBehavior viewBehavior = userPostBehaviorMapper.selectOne(userId, postId, UserPostBehaviorEnum.VIEW.getType());

            //如果不为空，说明用户已经浏览过帖子，则什么都不需要做
            if (viewBehavior != null) {
                return 0;
            }

            //到这里，说明用户没有浏览该帖子，则需要新增浏览行为，且浏览人数得+1
            viewBehavior = new UserPostBehavior();
            viewBehavior.setUserId(userId);
            viewBehavior.setPostId(postId);
            viewBehavior.setBehavior(UserPostBehaviorEnum.VIEW.getType());
            userPostBehaviorMapper.insert(viewBehavior);

            //浏览人数+1
            String key = PostRedisConstant.POST_VIEW_USER + postId;
            redis.incr(key);
        } catch (Exception e) {
            logger.error("incrViewPersonNum occur error, error = {}", e);
            throw e;
        }
        return 1;
    }

    /**
     * 浏览数 +1，浏览数只能加，不能减
     *
     * @param postId
     * @return
     */
    @Override
    public Integer incrViewNum(Long postId) {
        String key = PostRedisConstant.POST_VIEW + postId;
        redis.incr(key);
        return 1;
    }
}
